package com.shycloud.mido.common.pay.wxpay.utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class SHA256 {


    /**
     * 将加密后的字节数组转换成字符串
     *
     * @param b 字节数组
     * @return 字符串
     */
    public  static String byteArrayToHexString(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b!=null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1)
                hs.append('0');
            hs.append(stmp);
        }
        return hs.toString().toLowerCase();
    }
    /**
     * sha256_HMAC加密
     * @param message 消息
     * @param secret  秘钥
     * @return 加密后字符串
     */
    public static String sha256Encode(String origin, String charsetname, String secret) {
        String resultString = "";
        try {
            resultString = new String(origin);
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            if (charsetname == null || "".equals(charsetname)){
                byte[] bytes = sha256_HMAC.doFinal(origin.getBytes());
                resultString = byteArrayToHexString(bytes);
                //resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            }else{
                byte[] bytes = sha256_HMAC.doFinal(origin.getBytes(charsetname));
                resultString = byteArrayToHexString(bytes);
               // resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
            }
            // Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            // SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            // sha256_HMAC.init(secret_key);
            // byte[] bytes = sha256_HMAC.doFinal(origin.getBytes());
            // hash = byteArrayToHexString(bytes);
        } catch (Exception e) {
            System.out.println("Error HmacSHA256 ===========" + e.getMessage());
        }
        return resultString;
    }


}

